package org.robot.work.web20190114;

import java.util.List;
import java.util.stream.IntStream;

import org.jsoup.helper.StringUtil;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.robot.Spider;
import org.robot.component.Page;
import org.robot.dao.AuthorDao;
import org.robot.dao.IssueDao;
import org.robot.dao.JournalDao;
import org.robot.dao.PaperDao;
import org.robot.pojo.Author;
import org.robot.pojo.Issue;
import org.robot.pojo.Journal;
import org.robot.pojo.Paper;
import org.robot.utils.RegexUtil;

/**
 * 已更新
 * @author pdz
 * @date 2019年1月10日
 */
public class Jmd extends Spider {
	private static final Long TASKID = 1004L;

	@Override
	public void parse(Page page) throws Exception {
		if (page.isSeed()) {
//			parseJournal(page);
			page.setNextLinksFilter(e -> RegexUtil.getYear(e.text()).matches("201[6-9]"));
		} else if (page.typeEquals("issue")) {
//			parseIssue(page);
		} else if (page.typeEquals("paper")) {
//			parsePaper(page);
			parsePaperInfo(page);
		}
	}
	

	private void parsePaperInfo(Page page) {
		Long paperid = PaperDao.selectPidByWebsite(Paper.class, page.url(), TASKID);
		Elements list = page.select("meta[name^=citation_author]");
		int[] indexArr = IntStream.range(0, list.size()).filter(i -> list.get(i).attr("name").equals("citation_author")).toArray();
		for (int i = 0; i < indexArr.length; i++) {
			int start = indexArr[i];
			int end = i == indexArr.length-1 ? list.size()-1 : indexArr[i+1];
			parseAut(start, end, list, paperid);
		}
	}


	private void parseAut(int start, int end, Elements list, Long paperid) {
		String aut = null;
		String email = null;
		String aff = "";
		for (int i = start; i < end; i++) {
			Element ele = list.get(i);
			String nameAttr = ele.attr("name");
			String val = ele.attr("content");
			if (nameAttr.equals("citation_author"))
				aut = val;
			else if (nameAttr.equals("citation_author_email"))
				email = val;
			else if (nameAttr.equals("citation_author_institution"))
				aff += "^" + val;
		}
		aff = aff.replaceFirst("\\^", "");
		Author author = new Author();
		author.setFullName(aut);
		author.setEmail(email);
		author.setTaskId(TASKID);
		author.setPaperid(paperid);
		author.setAff(aff);
		AuthorDao.addAuthor(author);
	}

	private void parsePaper(Page page) {
		String title = page.urlText();
		String doi = page.select("meta[name=citation_doi]").attr("content");
		String pdfUrl = page.select("meta[name=citation_pdf_url]").attr("content");
		String abst = page.select("meta[property=og:description]").attr("content");
		Long issueid = IssueDao.selectPidByWebsite(Issue.class, page.prevUrl(), TASKID);
		
		Paper paper = new Paper();
		paper.setWebsite(page.url());
		paper.setTitle(title);
		paper.setTaskid(TASKID);
		paper.setDoi(doi);
		paper.setAbstracts(abst);
		paper.setPdfUrl(pdfUrl);
		paper.setIssueId(issueid);
		PaperDao.addPaper(paper);
	}

	private void parseIssue(Page page) {
		Long journalid = JournalDao.selectPidByWebsite(Journal.class, page.prevUrl(), TASKID);
		String issueVal = page.urlText();
		String year = RegexUtil.getYear(issueVal);
		Issue issue = new Issue();
		issue.setIssue(issueVal);
		issue.setYear(year);
		issue.setTaskId(TASKID);
		issue.setWebsite(page.url());
		issue.setJournalId(journalid);
		IssueDao.addIssue(issue);
	}

	private void parseJournal(Page page) {
		Journal journal = new Journal();
		journal.setWebsite(page.url());
		journal.setIssn("1525-1578");
		journal.setTaskId(TASKID);
		journal.setJournalname("The Journal of Molecular Diagnostics");
		JournalDao.addJournal(journal);
	}

	public static void main(String[] args) {
		Jmd claw = new Jmd();
		claw.setBaseDirectory("E:/webSpider/Jmd");
		claw.addSeed("https://jmd.amjpathol.org/issues");
		claw.addRegex("https://jmd.amjpathol.org/issue/.+", "issue");
		claw.addRule(".articleTitle a", "https://jmd.amjpathol.org/article/.+/fulltext", "paper");
		claw.start();
		
		
	}

}
